/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataConverters.DataFormatters.formattersimpl;

import DataConverters.DataFormatters.AbstractDataFormatter;
import DataConverters.exceptions.DataConvertException;
import DataConverters.interfaces.IConvertable;
import DataConverters.interfaces.IConvertableFile;
import DataConverters.interfaces.dataformulaobject.intefaces.BooleanCell;
import DataConverters.interfaces.dataformulaobject.intefaces.BooleanFormulaCell;
import DataConverters.interfaces.dataformulaobject.intefaces.Cell;
import DataConverters.interfaces.dataformulaobject.intefaces.CellType;
import DataConverters.interfaces.dataformulaobject.intefaces.DateCell;
import DataConverters.interfaces.dataformulaobject.intefaces.DateFormulaCell;
import DataConverters.interfaces.dataformulaobject.intefaces.NumberCell;
import DataConverters.interfaces.dataformulaobject.intefaces.NumberFormulaCell;
import DataConverters.interfaces.dataformulaobject.intefaces.StringFormulaCell;
import java.io.IOException;
import java.util.SortedSet;
import java.util.TreeSet;
import jxl.Workbook;
import jxl.biff.formula.FormulaException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 *
 * @author Nobody
 */
public class Excel97Formatter extends AbstractDataFormatter {

    public Excel97Formatter(SortedSet<Cell> list) {
        super(list);
    }

    public Excel97Formatter() {
        super(new TreeSet<Cell>());
    }

    @Override
    public void format(IConvertable source) throws DataConvertException {
        IConvertableFile fileSource = (IConvertableFile) source;
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(fileSource.asFile());
            WritableSheet sheet = workbook.createSheet("Sheet 1", 0);
            try {
                for (Cell cell : list) {

                    int j = cell.getColumn();
                    int i = cell.getRow();
                    try {
                        if (cell.getType().equals(CellType.BOOLEAN)) {
                            BooleanCell booleanCell = (BooleanCell) cell;
                            sheet.addCell(new jxl.write.Boolean(j, i, booleanCell.getValue()));
                        } else if (cell.getType().equals(CellType.BOOLEAN_FORMULA)) {
                            BooleanFormulaCell booleanFormulaCell = (BooleanFormulaCell) cell;
                            sheet.addCell(new jxl.write.Formula(j, i, booleanFormulaCell.getFormula()));
                        } else if (cell.getType().equals(CellType.DATE)) {
                            DateCell dateCell = (DateCell) cell;
                            sheet.addCell(new jxl.write.DateTime(j, i, dateCell.getDate()));
                        } else if (cell.getType().equals(CellType.DATE_FORMULA)) {
                            DateFormulaCell dateFormulaCell = (DateFormulaCell) cell;
                            sheet.addCell(new jxl.write.Formula(j, i, dateFormulaCell.getFormula()));
                        } else if (cell.getType().equals(CellType.ERROR)) {
                            sheet.addCell(new jxl.write.Label(j, i, "#Error"));
                        } else if (cell.getType().equals(CellType.FORMULA_ERROR)) {
                            sheet.addCell(new jxl.write.Label(j, i, "#FormulaError"));
                        } else if (cell.getType().equals(CellType.LABEL)) {
                            sheet.addCell(new jxl.write.Label(j, i, cell.getContents()));
                        } else if (cell.getType().equals(CellType.NUMBER)) {
                            NumberCell numberCell = (NumberCell) cell;
                            sheet.addCell(new jxl.write.Number(j, i, numberCell.getValue()));
                        } else if (cell.getType().equals(CellType.NUMBER_FORMULA)) {
                            NumberFormulaCell numberFormulaCell = (NumberFormulaCell) cell;
                            sheet.addCell(new jxl.write.Formula(j, i, numberFormulaCell.getFormula()));
                        } else if (cell.getType().equals(CellType.STRING_FORMULA)) {
                            StringFormulaCell stringFormulaCell = (StringFormulaCell) cell;
                            sheet.addCell(new jxl.write.Label(j, i, stringFormulaCell.getFormula()));
                        }
                    } catch (FormulaException ex) {
                        sheet.addCell(new jxl.write.Label(j, i, ex.getMessage()));
                    }
                }
                workbook.write();
                workbook.close();
            } catch (WriteException ex) {
                throw new DataConvertException(ex.getMessage(), ex);
            }
        } catch (IOException ex) {
            throw new DataConvertException(ex.getMessage(), ex);
        }

    }
}
